package com.csoa.community.utils;

import java.util.Iterator;

import org.json.chinaface.JSONArray;
import org.json.chinaface.JSONObject;



public class Json {
	JSONObject root=null;
	/**
	 * ������Ĭ�ϲ����json
	 * @param apiVersion
	 */
//	public Json(int apiVersion){
//		try {
//			JSONObject common=new JSONObject(Configs.typeAndVsersion);
//			common.put("apiversion",apiVersion);
//			root=new JSONObject();
//			root.put("common", common);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
	/**
	 * ����ָ��json
	 * @param jsonString
	 */
	public Json(String jsonString){
		try {
			root=new JSONObject(jsonString);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * ����jsonʱ,����ָ��json
	 * @param jsonObject
	 */
	public Json(JSONObject jsonObject){
		root=jsonObject;
	}
	
	/**
	 * ��������ݵ�json
	 */
	public Json(){
		root=new JSONObject();
	}
	public boolean put(String key,int value){
		try {
			root.put(key, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public boolean put(String key,Object value){
		try {
			root.put(key, value);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
//	@Override
//	public String toString() {
//		if(root==null){
//			return null;
//		}
//		String rootString=root.toString();
////		LogInfo.LogOut("before encode json="+rootString);
//		rootString=XmlBase64.encode(rootString.getBytes());
//		return "?request="+rootString+"&sign="+MD5.md5Lower(rootString+"1731c73ef747457e8ac6f2ddb7de9227087e337ee96b4545b71edd50ea79d367")+"&type=52f78ffbda1e416e";
//	}
	public String toNormalString(){
		if(root==null){
			return null;
		}
		return root.toString();
	}
	public String getString(String key){
		try {
			if(root.has(key)){
				return root.get(key).toString();
			}else{
//				LogInfo.LogOut("not has String key:"+key);
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	public int getInt(String key){
		try {
			if(root.has(key)){
				return root.getInt(key);
			}else{
//				LogInfo.LogOut("not has int key:"+key);
				return -1;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	public boolean getBoolean(String key){
		try {
			if(root.has(key)){
				return root.getBoolean(key);
			}else{
//				LogInfo.LogOut("not has boolean key:"+key);
				return false;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public Json getJson(String key){
		try {
			if(root.has(key)){
				return new Json(root.getJSONObject(key));
			}else{
//				LogInfo.LogOut("not has json key:"+key);
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public Json[] getJsonArray(String key){
		Json[] jsons;
		try {
			if(!root.has(key)){
//				LogInfo.LogOut("not has jsonArray key:"+key);
				return null;
			}
			JSONArray a = root.getJSONArray(key);
			jsons=new Json[a.length()];
			for(int i=0;i<jsons.length;i++){
				jsons[i]=new Json(a.getJSONObject(i));
			}
			return jsons;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public Object getObject(String key){
		try {
			if(root.has(key)){
				return root.get(key);
			}else{
//				LogInfo.LogOut("not has int key:"+key);
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public Iterator<String> keys() {
		return root.keys();
	}
	
}
